﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TxtManager.Application.Mapping;
using TxtManager.Application.Services;
using TxtManager.Core.Entities;
using TxtManager.Core.Repository;
using TxtManager.Infrastructure.Context;
using TxtManager.Infrastructure.Repository;
using TxtManager.WPF.ViewModels;

namespace TxtManager.WPF;

public class Startup
{

    public static IServiceProvider ConfigureServices()
    {
        var services = new ServiceCollection();

        // 数据库
        services.AddDbContext<TextDbContext>(options =>
            options.UseSqlite("Data Source=texts.db"));



        /*
         *
         * // Scrutor自动注册（核心修改部分）
            services.Scan(scan => scan
                // 注册仓储实现
                .FromAssemblyOf<EfTextRepository>() // Infrastructure层程序集
                  .AddClasses(classes => classes.AssignableTo<IRepository<,>>())
                  .AsImplementedInterfaces()
                  .WithScopedLifetime()

                // 注册Application层服务
                .FromAssemblyOf<TextService>() // Application层程序集
                  .AddClasses(classes => classes.Where(t => t.Name.EndsWith("Service")))
                  .AsSelfWithInterfaces()
                  .WithScopedLifetime()

                // 注册ViewModels
                .FromAssemblyOf<MainWindowViewModel>() // WPF层程序集
                  .AddClasses(classes => classes.AssignableTo<ObservableObject>())
                  .AsSelf()
                  .WithTransientLifetime()
            );
         *
         */

        services.AddScoped<IRepository<TextItem, int>, EfTextRepository>();

        // Application服务
        services.AddScoped<TextService>();

        // AutoMapper
        services.AddAutoMapper(typeof(ApplicationProfile));

        // ViewModels
         services.AddTransient<MainWindowViewModel>();


        return services.BuildServiceProvider();
    }


}